package com.yanqu.road.server.cmd.tradewar.team;

import com.yanqu.road.entity.config.battle.BattleResult;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.tradewar.team.TradeWarTeamProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarTeamModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.player.TradeWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_TRADE_WAR_TEAM_BATTLE, desc = "商战队伍出战")
public class TradeWarTeamBattleCmd extends AbstractCommand {

	private List<BattleResult> resultList;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(player.getTitleId() < GameConfig.TRADE_WAR_TEAM_OPEN_LEVEL &&
				VipMgr.getVipLv(player.getUserInfo().getVipExp()) < GameConfig.TRADE_WAR_TEAM_OPEN_VIP){
			if(player.getTitleId() < GameConfig.TRADE_WAR_TEAM_OPEN_LEVEL) {
				ret = GameErrorCode.E_TITLE_LEVEL_NO_ENOUGH;
				return;
			}else {
				ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
				return;
			}
		}
		TradeWarTeamModule tradeWarTeamModule = player.getModule(TradeWarTeamModule.class);
		if(null == tradeWarTeamModule.getUserTradeWarTeam()){//系统未解锁
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(tradeWarTeamModule.getTeamPatronsCount() <= 0){
			ret = GameErrorCode.E_TRADE_WAR_TEAM_PATRONS_COUNT_NO_ENOUGH;
			return;
		}
		resultList = player.getModule(TradeWarTeamModule.class).tradeWarTeamBattle();
	}

	@Override
	public void sendData(GamePlayer player) {
		TradeWarTeamProto.TradeWarTeamBattleRespMsg.Builder respMsg = TradeWarTeamProto.TradeWarTeamBattleRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			for(BattleResult battleResult : resultList){
				TradeWarMgr.buildWarTeamBattleTemp(respMsg, battleResult);
			}
		}
		player.sendPacket(Protocol.U_TRADE_WAR_TEAM_BATTLE, respMsg);
	}
}
